﻿// For an introduction to the Page Control template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232511
(function () {
    "use strict";

    WinJS.UI.Pages.define("/pages/sessionpage/sessionpage.html", {
        init: function (element, options) {
            this.itemInvoked = WinJS.UI.eventHandler(this._itemInvoked.bind(this));
        },

        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.
        },

        unload: function () {
            // TODO: Respond to navigations away from this page.
        },

        updateLayout: function (element) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in layout.
        },
         _itemInvoked: function (args) {
             var item = Data.items.getAt(args.detail.itemIndex);
            Windows.Storage.ApplicationData.current.localSettings.values["selectFunctionTitle"] = item.CustomName;
             // Windows.Storage.ApplicationData.current.localSettings.values["selectModuleName"] = item.CustomName;

            selectfunction(item);

        }
    });
})();


var list1 = new WinJS.Binding.List();
var groupedItems = list1.createGrouped(
    function groupKeySelector(item) { return item.group.FunctionID; },
    function groupDataSelector(item) { return item.group; }
);

var Data = {
    module: WinJS.Binding.as({ Title: Windows.Storage.ApplicationData.current.localSettings.values["selectModuleName"] }),
    items: groupedItems,
    groups: groupedItems.groups
}


$(window).load(function () {
    $(".groupeditemslist").width($(window).width() - 120);
    $('.win-navigation-backbutton').click(function () {
        GoBack();
    })

    //$("#loading").hide();
    var m = "AD";
    if (Windows.Storage.ApplicationData.current.localSettings.values.hasKey("selectModule"))
        m = Windows.Storage.ApplicationData.current.localSettings.values["selectModule"];

    var data = sysAD;
    if (m == "CF")
        data = sysCF
    else if (m == "OP")
        data = sysOP
    else if (m == "CS")
        data = sysCS
    else if (m == "PP")
        data = sysPP
    else if (m == "SR")
        data = sysSR
    else if (m == "IV")
        data = sysIV
    else if (m == "GL")
        data = sysGL
    else if (m == "MI")
        data = sysMI
    else if (m == "CT")
        data = sysCT
    else if (m == "FA")
        data = sysFA
    else if (m == "IL")
        data = sysIL

    var groupitem = [];

    for (var i = 0; i < data.length; i++) {
        if (data[i].ParentID == data[i].Module)
            groupitem.push(data[i]);
    }

    for (var i = 0; i < data.length; i++) {
        for (var j = 0; j < groupitem.length; j++) {
            if (data[i].ParentID != data[i].Module && data[i].ParentID == groupitem[j].FunctionID) {
                data[i].group = groupitem[j];
                list1.push(data[i]);
            }
        }

    }


    WinJS.Binding.processAll(null, Data);
    HideBusy('busy');
})

